//
//  EmployerViewController.m
//  MagicZhao
//
//  Created by hy002 on 2017/12/22.
//  Copyright © 2017年 HUAYU. All rights reserved.
//

#import "EmployerViewController.h"
#import "CDZStarsControl.h"
#import "JobsModel.h"
#import "ScoringModel.h"
#import "WorkDetailsViewController.h"
#import "WorkerModel.h"
#import "FeedBackViewController.h"

static NSString *cellPath = @"EmployerCell";
@interface EmployerViewController ()<UITableViewDelegate,UITableViewDataSource,CDZStarsControlDelegate>
{
    UIView *lineBottomV;

}
@property (nonatomic, strong)UITableView *tableview;
@property (nonatomic, assign)NSInteger infoType;
@property (nonatomic, strong)NSMutableArray *orderAry;
@property (nonatomic, strong)NSMutableArray *scoringAry;
@property (nonatomic, assign)NSInteger leftPage;
@property (nonatomic, assign)NSInteger rightPage;
@property (nonatomic, strong)WorkerModel *worModel;
@property (nonatomic, strong)NSArray *titArray;

@end

@implementation EmployerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _infoType = 0;
    _leftPage = 0;
    _rightPage = 0;
    _orderAry = [NSMutableArray arrayWithCapacity:0];
    _scoringAry = [NSMutableArray arrayWithCapacity:0];
    _titArray = @[@"公司logo",@"公司名称",@"ID",@"常驻地",@"负责人",@"联系电话",@"联系地址"];
    [self creatUI];
    [self requestEmployerSelect];
    [self requestRecruitmentNotice];
    [self requestEvaluateList];
}

- (void)creatUI
{
    self.navigationController.navigationBar.translucent = NO;
    self.navigationItem.title = @"雇主信息";
    self.view.backgroundColor = BacksColor;
    self.tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-STA_NAV_HEIGHT-SCREER_HeiX) style:UITableViewStylePlain];
    _tableview.delegate = self;
    _tableview.dataSource = self;
    _tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableview.backgroundColor = BacksColor;
    [self.view addSubview:_tableview];
    self.tableview.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewHeaderData)];
    self.tableview.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreFootData)];
    self.tableview.mj_footer.automaticallyHidden = YES;
    self.tableview.estimatedRowHeight = 0;
    self.tableview.estimatedSectionHeaderHeight = 0;
    self.tableview.estimatedSectionFooterHeight = 0;
    
}

- (void)loadNewHeaderData
{
    if (_infoType == 0)
    {
        [_tableview.mj_header endRefreshing];
        [_tableview.mj_footer endRefreshing];
        return;
    }
    else if (_infoType == 1)
    {
        _leftPage = 0;
        [self requestRecruitmentNotice];
    }
    else
    {
        _rightPage = 0;
        [self requestEvaluateList];
        
    }
    
}

- (void)loadMoreFootData
{
    if (_infoType == 0)
    {
        [_tableview.mj_header endRefreshing];
        [_tableview.mj_footer endRefreshing];
        return;
    }
    else if (_infoType == 1)
    {
        _leftPage ++;
        [self requestRecruitmentNotice];
        
    }
    else
    {
        _rightPage ++;
        [self requestEvaluateList];
        
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (_infoType == 0)
    {
        return _titArray.count;
    }
    if (_infoType == 1)
    {
        return _orderAry.count;
    }
    if (_infoType == 2)
    {
        return _scoringAry.count;
    }
    
    return 10;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if (!cell)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellPath];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    for (UIView *subView in cell.contentView.subviews)
    {
        [subView removeFromSuperview];
    }
    if (_infoType == 0)
    {
        if (_worModel)
        {
            NSArray *rightAry = @[@"",_worModel.user.name,_worModel.ID,[NSString stringWithFormat:@"%@%@%@",_worModel.user.province,_worModel.user.city,_worModel.user.areaName],_worModel.linkman,_worModel.phone,_worModel.address];
            if (indexPath.row != 0)
            {
                UIView *liView = [[UIView alloc] initWithFrame:CGRectMake(15*displaywidth, 0, SCREEN_WIDTH-15*displaywidth, 1*displaywidth)];
                liView.backgroundColor = LinesColor;
                [cell.contentView addSubview:liView];
                UILabel *rightLab = [ZCControl createLabelWithFrame:CGRectMake(100*displaywidth, 1*displaywidth, SCREEN_WIDTH-115*displaywidth, 44*displaywidth) Font:12.0 Text:rightAry[indexPath.row] Color:RGB(179, 179, 179)];
                if (indexPath.row == 1) {
                    rightLab.text = [NSString stringWithFormat:@"%@PE", rightAry[indexPath.row]];
                }
                rightLab.textAlignment = NSTextAlignmentRight;
                [cell.contentView addSubview:rightLab];
            }
            else
            {
                UIImageView *faceimgView = [ZCControl createImageViewWithFrame:CGRectMake(SCREEN_WIDTH-95*displaywidth, 7.5*displaywidth, 75*displaywidth, 75*displaywidth) ImageName:@"zhanwei"];
                [faceimgView sd_setImageWithURL:URL(_worModel.user.headImg)];
                faceimgView.layer.cornerRadius = 37.5*displaywidth;
                faceimgView.layer.masksToBounds = YES;
                [cell.contentView addSubview:faceimgView];
                
            }
            cell.textLabel.text = _titArray[indexPath.row];
            cell.textLabel.font = NormalFontWithSize(14.0+fontScale);
            cell.textLabel.textColor = RGB(77, 77, 77);
        }
        
        
    }
    else if (_infoType == 1)
    {
        cell.backgroundColor = BacksColor;
        UIView *whiteview = [[UIView alloc] initWithFrame:CGRectMake(5*displaywidth, 5*displaywidth, SCREEN_WIDTH-10*displaywidth, 125*displaywidth)];
        whiteview.backgroundColor = [UIColor whiteColor];
        whiteview.layer.cornerRadius = 5;
        whiteview.layer.masksToBounds = YES;
        [cell.contentView addSubview:whiteview];
        if (_orderAry.count > 0)
        {
            JobsModel *model = _orderAry[indexPath.row];
            UILabel *nameLab = [ZCControl createLabelWithFrame:CGRectMake(20*displaywidth, 15*displaywidth, SCREEN_WIDTH-80*displaywidth, 25*displaywidth) Font:16.0 Text:model.name Color:RGB(129, 148, 196)];
            [whiteview addSubview:nameLab];
            UILabel *tostLab = [ZCControl createLabelWithFrame:CGRectMake(20*displaywidth, 40*displaywidth, SCREEN_WIDTH-80*displaywidth, 50*displaywidth) Font:13.0 Text:[NSString stringWithFormat:@"生产说明：\n%@",model.remark] Color:RGB(77, 77, 77)];
            tostLab.lineBreakMode = NSLineBreakByTruncatingTail;
            tostLab.adjustsFontSizeToFitWidth = NO;
            [whiteview addSubview:tostLab];
            UILabel *timeLab = [ZCControl createLabelWithFrame:CGRectMake(20*displaywidth, 90*displaywidth, SCREEN_WIDTH-80*displaywidth, 20*displaywidth) Font:12.0 Text:[NSString stringWithFormat:@"项目工期：%@至%@",model.startDate,model.endDate] Color:RGB(128, 128, 128)];
            [whiteview addSubview:timeLab];
            UILabel *detailLab = [ZCControl createLabelWithFrame:CGRectMake(SCREEN_WIDTH-125*displaywidth, 90*displaywidth, 100*displaywidth, 20*displaywidth) Font:13.0 Text:@"查看详情" Color:RGB(236, 165, 84)];
            detailLab.textAlignment = NSTextAlignmentRight;
            [whiteview addSubview:detailLab];
//            UIButton *detailBtn = [ZCControl createButtonWithFrame:CGRectMake(SCREEN_WIDTH-125*displaywidth, 90*displaywidth, 100*displaywidth, 20*displaywidth) ImageName:nil Target:self Action:@selector(detailAction:) Title:@"查看详情" Font:13.0];
//            detailBtn.backgroundColor = [UIColor whiteColor];
//            detailBtn.tag = 100+indexPath.row;
//            [detailBtn setTitleColor:RGB(236, 165, 84) forState:UIControlStateNormal];
//            detailBtn.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
//            detailBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
//            [whiteview addSubview:detailBtn];
            NSInteger statusNum = [model.status integerValue];
            UIImageView *typeImgView = [ZCControl createImageViewWithFrame:CGRectMake(SCREEN_WIDTH-45*displaywidth, 5*displaywidth, 40*displaywidth, 35*displaywidth) ImageName:@"jinxingzhong"];
            typeImgView.contentMode = UIViewContentModeScaleAspectFit;
            typeImgView.hidden = YES;
            [cell.contentView addSubview:typeImgView];
            if (statusNum == 2)
            {
                typeImgView.hidden = NO;
            }
            if (statusNum == 3)
            {
                typeImgView.hidden = NO;
                typeImgView.frame = CGRectMake(SCREEN_WIDTH-60*displaywidth, 35*displaywidth, 45*displaywidth, 45*displaywidth);
                typeImgView.image = [UIImage imageNamed:@"yiwancheng"];
            }
        }
        
        
        
    }
    else
    {
        if (_scoringAry.count > 0)
        {
            ScoringModel *model = _scoringAry[indexPath.row];
            UIView *lineBview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 1*displaywidth)];
            lineBview.backgroundColor = LinesColor;
            [cell.contentView addSubview:lineBview];
            UIImageView *imgView = [ZCControl createImageViewWithFrame:CGRectMake(10*displaywidth, 15*displaywidth, 40*displaywidth, 40*displaywidth) ImageName:@"zhanwei"];
            imgView.layer.cornerRadius = 20*displaywidth;
            imgView.layer.masksToBounds = YES;
            if (model.user.headImg.length > 0)
            {
                [imgView sd_setImageWithURL:[NSURL URLWithString:model.user.headImg] placeholderImage:[UIImage imageNamed:@"zhanwei"]];
            }
            [cell.contentView addSubview:imgView];
            NSInteger typeNum = [model.user.type integerValue];
            NSString *typeName = @"雇主";
            UIColor *backColor = RGB(238, 103, 94);
            if (typeNum == 2)
            {
                typeName = @"厂家";
                backColor = ThemeColor;
            }
            if (typeNum == 3)
            {
                typeName = @"工人";
                backColor = ThemeColor;
            }
            UILabel *typrLab = [ZCControl createLabelWithFrame:CGRectMake(60*displaywidth, 15*displaywidth, 30*displaywidth, 15*displaywidth) Font:10.0 Text:typeName Color:[UIColor whiteColor]];
            typrLab.backgroundColor = backColor;
            typrLab.textAlignment = NSTextAlignmentCenter;
            typrLab.layer.cornerRadius = 2;
            typrLab.layer.masksToBounds = YES;
            [cell.contentView addSubview:typrLab];
            
            UILabel *nameLab = [ZCControl createLabelWithFrame:CGRectMake(105*displaywidth, 10*displaywidth, SCREEN_WIDTH-215*displaywidth, 25*displaywidth) Font:12.0 Text:model.user.name Color:RGB(153, 153, 153)];
            [cell.contentView addSubview:nameLab];
            
            UILabel *contenLab = [ZCControl createLabelWithFrame:CGRectMake(105*displaywidth, 35*displaywidth, SCREEN_WIDTH-120*displaywidth, 35*displaywidth) Font:14.0 Text:model.user.name Color:RGB(77, 77, 77)];
            [cell.contentView addSubview:contenLab];
            
            CDZStarsControl *starsControl = [CDZStarsControl.alloc initWithFrame:CGRectMake(SCREEN_WIDTH-110*displaywidth, 10*displayheight, 100*displaywidth , 25*displayheight) stars:5 starSize:CGSizeMake(20*displayheight, 20*displayheight) noramlStarImage:[UIImage imageNamed:@"huixx"] highlightedStarImage:[UIImage imageNamed:@"hongxx"]];
            starsControl.delegate = self;
            starsControl.allowFraction = YES;
            starsControl.score = [model.grade floatValue];
            [cell.contentView addSubview:starsControl];
            
        }
    }
    
        
    
    
    return cell;
}

////查看详情
//- (void)detailAction:(UIButton *)btn
//{
//    //    NSInteger index = btn.tag-100;
//    
//}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_infoType == 1)
    {
        JobsModel *model = _orderAry[indexPath.row];
        WorkDetailsViewController *vc = [[WorkDetailsViewController alloc] init];
        vc.projectID = model.ID;
        vc.deatilType = 3;
        [self.navigationController pushViewController:vc animated:YES];
    }
    if (_infoType == 2)
    {
        UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"请选择举报理由" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
        
        [alertVC addAction:[UIAlertAction actionWithTitle:@"低俗脏话" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            FeedBackViewController *vc = [[FeedBackViewController alloc] init];
            vc.titleStr = @"举报";
            [self.navigationController pushViewController:vc animated:YES];
        }]];
        
        [alertVC addAction:[UIAlertAction actionWithTitle:@"营销诈骗" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            FeedBackViewController *vc = [[FeedBackViewController alloc] init];
            vc.titleStr = @"举报";
            [self.navigationController pushViewController:vc animated:YES];
        }]];
        [alertVC addAction:[UIAlertAction actionWithTitle:@"其他理由" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            FeedBackViewController *vc = [[FeedBackViewController alloc] init];
            vc.titleStr = @"举报";
            [self.navigationController pushViewController:vc animated:YES];
        }]];
        
        [alertVC addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
            
        }]];
        
        
        [self presentViewController:alertVC animated:YES completion:nil];
    }
}



- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *myView = [[UIView alloc] init];
    myView.frame = CGRectMake(0, 0, SCREEN_WIDTH, 45*displayheight);
    myView.backgroundColor = BacksColor;
    UIView *whiteView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 40*displaywidth)];
    whiteView.backgroundColor = [UIColor whiteColor];
    [myView addSubview:whiteView];
    NSArray *titArray = @[@"信息",@"项目",@"评价"];
    CGFloat width = (SCREEN_WIDTH-2)/3;
    for (int i = 0; i < titArray.count; i++)
    {
        UIButton *btn = [ZCControl createButtonWithFrame:CGRectMake((width+1)*i, 0, width, 39*displayheight) ImageName:nil Target:self Action:@selector(closeInfoAction:) Title:titArray[i] Font:16.0];
        btn.backgroundColor = [UIColor whiteColor];
        [btn setTitleColor:RGB(77, 77, 77) forState:UIControlStateNormal];
        if (i == _infoType)
        {
            [btn setTitleColor:ThemeColor forState:UIControlStateNormal];
        }
        btn.tag = 20+i;
        [whiteView addSubview:btn];
    }
    
    UIView *lineBV = [[UIView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH/3*_infoType+40*displaywidth, 39*displayheight, SCREEN_WIDTH/3-80*displaywidth, 2*displayheight)];
    lineBV.backgroundColor = RGB(238, 103, 94);
    lineBV.layer.cornerRadius = 1*displaywidth;
    lineBV.layer.masksToBounds = YES;
    [myView addSubview:lineBV];
    lineBottomV = lineBV;
    
    
    return myView;
}

- (void)closeInfoAction:(UIButton *)btn
{
    _infoType = btn.tag-20;
    lineBottomV.frame = CGRectMake(SCREEN_WIDTH/3*_infoType+40*displaywidth, 44*displayheight, SCREEN_WIDTH/3-80*displaywidth, 2*displayheight);
    [self.tableview reloadData];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 45*displayheight;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_infoType == 0)
    {
        if (indexPath.row == 0)
        {
            return 90*displaywidth;
        }
        return 45*displaywidth;
    }
    else if (_infoType == 1)
    {
        return 130*displaywidth;
    }
    else
    {
        return 70*displaywidth;
    }
}




- (void)requestRecruitmentNotice
{
    WEAKSELF;
    NSDictionary *dic = @{@"page":[NSString stringWithFormat:@"%ld",_leftPage],
                          @"employer.id":[NSString stringWithFormat:@"%@",_empID],
                          @"status":@"1"
                          };
    [HYBaseHandler requestRecruitmentNoticeParmers:dic Success:^(id obj) {
        [weakSelf.tableview.mj_header endRefreshing];
        [weakSelf.tableview.mj_footer endRefreshing];
        if ([obj server_Getcodecheck])
        {
            NSArray *orderArray = [JobsModel mj_objectArrayWithKeyValuesArray:obj[@"data"][@"content"]];
            dispatch_async(dispatch_get_main_queue(), ^{
                if (weakSelf.leftPage == 0)
                {
                    [weakSelf.orderAry removeAllObjects];
                }
                [weakSelf.orderAry addObjectsFromArray:orderArray];
                [weakSelf.tableview reloadData];
            });
            
        }
        else
        {
            [LTools showMBProgressWithText:obj[@"msg"] addToView:weakSelf.view];
        }
    } failed:^(NSError *error) {
        [weakSelf.tableview.mj_header endRefreshing];
        [weakSelf.tableview.mj_footer endRefreshing];
    }];
}

- (void)requestEvaluateList
{
    WEAKSELF;
    NSDictionary *dic = @{@"page":[NSString stringWithFormat:@"%ld",_rightPage],
                          @"target.id":[NSString stringWithFormat:@"%@",_targetId]
                          };
    [HYBaseHandler requestEvaluateListParmers:dic Success:^(id obj) {
        [weakSelf.tableview.mj_header endRefreshing];
        [weakSelf.tableview.mj_footer endRefreshing];
        if ([obj server_Getcodecheck])
        {
            NSArray *scoringArray = [ScoringModel mj_objectArrayWithKeyValuesArray:obj[@"data"][@"content"]];
            dispatch_async(dispatch_get_main_queue(), ^{
                if (weakSelf.rightPage == 0)
                {
                    [weakSelf.scoringAry removeAllObjects];
                }
                [weakSelf.scoringAry addObjectsFromArray:scoringArray];
                [weakSelf.tableview reloadData];
            });
            
        }
        else
        {
            [LTools showMBProgressWithText:obj[@"msg"] addToView:weakSelf.view];
        }
    } failed:^(NSError *error) {
        [weakSelf.tableview.mj_header endRefreshing];
        [weakSelf.tableview.mj_footer endRefreshing];
    }];
}

- (void)requestEmployerSelect
{
    WEAKSELF;
    NSDictionary *dic = @{@"id":[NSString stringWithFormat:@"%@",_empID]};
    [HYBaseHandler requestEmployerSelectParmers:dic Success:^(id obj) {
        if ([obj server_Getcodecheck])
        {
            weakSelf.worModel = [WorkerModel mj_objectWithKeyValues:obj[@"data"]];
            dispatch_async(dispatch_get_main_queue(), ^{
                [weakSelf.tableview reloadData];
            });
        }
        else
        {
            [LTools showMBProgressWithText:obj[@"msg"] addToView:weakSelf.view];
        }
    } failed:^(NSError *error) {
        
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
